<?php

function addToCart($id,$price)
{
    include "../../db/dbconnect.php";
    //mysql_query("INSERT INTO cart (History_ID, QTY) VALUES (140,1)");
    $sql = "SELECT * FROM Shop WHERE iid = '{$id}'";
    $result = mysql_query($sql);
    //$dbrow = mysql_num_rows($result);
    
    $query = "SELECT * FROM cart WHERE Item_ID = '{$id}'";
    $cartResult = mysql_query($query);
    $drow = mysql_num_rows($cartResult);


    if($drow != 0)
    {
        $row = mysql_fetch_array($cartResult);
        $amount = $row['Qty'];
        $newamount= $amount+1;
        $name = $row['Item_Name'];
        $newprice = $newamount*$price;
        //echo $newprice;
        mysql_query("UPDATE cart SET TransactionValue ='{$newprice}' , Qty ='{$newamount}' WHERE Item_ID = {$id}");
       // echo "update";
    }
    else
    {
        $row = mysql_fetch_array($result);
        $id = $row['iid'];
       // echo $id;
        $name = $row['name'];
       // echo "addnew";
       // $img = $row['link'];
       // $price = $row['Price'];
        mysql_query("INSERT INTO cart (Item_ID,Item_Name, TransactionValue,Qty) VALUES ('{$id}','{$name}','{$price}','1')");
    }
}


function deleteToCart($id)
{
    include "../../db/dbconnect.php";

    $query = "SELECT * FROM cart WHERE Item_ID = '{$id}'";
    $cartResult = mysql_query($query);
    $drow = mysql_num_rows($cartResult);
    //echo $id;

    if($drow != 0)
    {
        mysql_query("DELETE FROM cart WHERE Item_ID='{$id}'");
       // echo "delete";
    }

}

 function updateCart($id,$qty)
 {
    if($qty<0)
    {
        echo "quality can not be less than 0";
    }
    else if ($qty==0){
       deleteToCart($id);
    }
    else{

    include "../../db/dbconnect.php";
    //echo $id;
    $sql = "SELECT * FROM Shop WHERE iid = '{$id}'";
    $result = mysql_query($sql);
    $dbrow = mysql_num_rows($result);
    if($dbrow != 0)
    {
        $rowShop = mysql_fetch_array($result);
        $single_price = $rowShop['Price'];

        $newprice = $qty*$single_price;

        mysql_query("UPDATE cart SET TransactionValue ='{$newprice}' , Qty ='{$qty}' WHERE Item_ID = {$id}");
    }
   }

 }
 function checkout()
        {
           $SQL = "SELECT * FROM cart";
           $result = mysql_query($SQL);
           while ($row = mysql_fetch_array($result))
           {
               $History_ID = $row['History_ID'];
               $BuyerID = $row['BuyerID'];
               $SellerID = $row['SellerID'];
               $Qty = $row['Qty'];
               $TransactionValue = $row['TransactionValue'];
               $Item_Name = $row['Item_Name'];
              // echo "asdfas";
               mysql_query("INSERT INTO PurchaseHistory (History_ID, BuyerID, SellerID, Qty, TransactionValue, Time, Item_Name)
                                               VALUES('$History_ID', '$BuyerID', '$SellerID', '$Qty', '$TransactionValue', NOW(), '$Item_Name') ");
               mysql_query("DELETE FROM cart WHERE cart. History_ID=$History_ID LIMIT 1");
           }
        }
?>
  